#!/usr/bin/python

import os, sys, urllib2
import lxml.html as html

response = urllib2.urlopen("http://codeforces.ru/contest/" + sys.argv[1] + "/problems?locale=en")
data = html.parse(response)

tasks = data.xpath("//div[@class='sample-tests']")

cnt = 1

for task in tasks:
  taskname = "Task" + str(cnt)
  cnt += 1
  os.mkdir(taskname)
  testsdir = taskname + "/tests/"
  expdir = taskname + "/expected/"
  outdir = taskname + "/output/"
  os.mkdir(testsdir)
  os.mkdir(expdir)
  os.mkdir(outdir)
  tests_input = task.xpath(".//div[@class='input']/pre")
  tests_output = task.xpath(".//div[@class='output']/pre")
  for i in range(len(tests_input)):
    input_data = html.tostring(tests_input[i])[5:-10].replace("<br>", "\n")
    output_data = html.tostring(tests_output[i])[5:-10].replace("<br>", "\n")
    input_f = open(testsdir + str(i + 1) + ".txt", "w")
    input_f.write(input_data)
    input_f.close()
    output_f = open(expdir + str(i + 1) + ".txt", "w")
    output_f.write(output_data)
    output_f.close()